class ChatControllerTests extends GroovyTestCase {

    void testTryingToGetChatLogFirstTime() {
		def c = new ChatController()
		c.params.id = 'user1'
		c.index()
		
		// test user
		assert User.count() == 1
		assert User.findByName('user1') != null
		
		// test returning result
		assert c.response.contentAsString == ''
    }

    void testTryingtoGetChatLogFirstTime_with_Message() {
		def c = new ChatController()
		c.params.id = 'user1'
		c.params.msg = 'test'
		c.index()
		
		// test user
		// println User.count()
		assert User.count() == 1
		def u = User.findByName('user1')
		assert u != null
		
		assert ChatLog.count() == 1		
		def cl = ChatLog.findByMsg('test')
		assert cl != null
		assert cl.user == u
		
		assert c.response.contentAsString == ''
    }
    
    void test_TwoUsersChat() {
    	// First, speaker chats
    	def cc_1 = new ChatController()
    	cc_1.params.id = 'speaker'
    	cc_1.params.msg = 'hello'
    	cc_1.index()
    	
    	// Second, person1 chats. He should see the text previously chat by the speaker
    	def cc_2 = new ChatController()
    	cc_2.params.id = 'person1'
    	cc_2.params.msg = 'hello, too'
    	cc_2.index()
    	
    	assert User.count() == 2
    	assert '<b>speaker</b>: hello\n' == cc_2.text
    	assert 'hello' == User.findByName('speaker').lastLog.msg
    	
    	def cc_3 = new ChatController()
    	cc_3.params.id = 'speaker'
    	cc_3.index()    	    	
    	assert 'hello, too' == User.findByName('speaker').lastLog.msg
    	assert '<b>person1</b>: hello, too\n' == cc_3.text
    }                
        
}
